//
//  CameraView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/4/10.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class CameraView: UIView,setImg {
    var deleteBtns : Array<Button> = []
    weak var helper : BaseViewController?
    override init(frame: CGRect) {
        super.init(frame: frame)
        let btn = UIButton.init(type: .custom)
        for _ in 0 ..< 3 {
            deleteBtns.append(btn)
        }
        self.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func load(title : String,item1 : String,item2 : String)->Void{
        let titleBack = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.width(), height: 88 * YXHeightScale))
        titleBack.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
        self.addSubview(titleBack)
        let font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        let titleSize = title.StringSize(font: font)
        let ti = UILabel.init(frame: CGRect.init(x: 42 * YXWidthScale, y: 30 * YXHeightScale, width: titleSize.width, height: titleSize.height))
        ti.font = font
        ti.textColor = UIColor.colorWithHexString(hex: "323232")
        ti.text = title
        titleBack.addSubview(ti)
        for i in 0..<3 {
            let button = UIButton.init(type: .custom)
            button.tag = 1000 + i
            let img = UIImage.load(name: "icon_camera")
            button.frame = CGRect.init(x: 63 * YXWidthScale + 208 * YXWidthScale * CGFloat(i), y: titleBack.bottom(), width: 188 * YXWidthScale, height: 187 * YXWidthScale)
            button.setImage(img, for: .normal)
            button.tintColor = UIColor.black
            button.imageEdgeInsets = UIEdgeInsetsMake(80 * YXHeightScale, 80 * YXWidthScale, 80 * YXHeightScale, 80 * YXWidthScale)
            button.layer.borderColor = UIColor.colorWithHexString(hex: "dfdfdf").cgColor
            button.layer.borderWidth = YXHeightScale
            button.addTarget(helper, action: #selector(GetInformationViewController.handle(idx:)), for: .touchUpInside)
            self.addSubview(button)
        }
    }
    
    func delete(idx: Int) {
        if let button = self.viewWithTag(1000 + idx) as? UIButton {
            let img = UIImage.load(name: "icon_camera")
            button.imageEdgeInsets = UIEdgeInsetsMake(80 * YXHeightScale, 80 * YXWidthScale, 80 * YXHeightScale, 80 * YXWidthScale)
            button.setImage(img, for: .normal)
            let delete = deleteBtns[idx]
            delete.removeFromSuperview()
        }
    }
    
    func image(img: UIImage,idx : Int) {
        if let button = self.viewWithTag(1000 + idx) as? UIButton {
            button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
            button.setImage(img, for: .normal)
            if self.viewWithTag(1010 + idx) != nil {
                
            } else {
                let delete = UIButton.init(type: .custom)
                let img1 = UIImage.load(name: "icon_delete")
                delete.frame = CGRect.init(x: button.right() - 18 * YXWidthScale, y: button.top() - 18 * YXWidthScale, width: 36 * YXWidthScale, height: 36 * YXHeightScale)
                delete.setImage(img1, for: .normal)
                delete.tag = 1010 + idx
                delete.addTarget(helper, action: #selector(GetInformationViewController.delt(idx:)), for: .touchUpInside)
                self.addSubview(delete)
                deleteBtns[idx] = delete
            }
        }
    }
    
}
